package com.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.User;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:PrivacyEditTask
 * @Description:TODO修改用户权限
 * @author zhuw
 * @date 2013-8-13 下午7:18:41
 */
public class UserPrivacyEditTask extends
		AsyncTask<HashMap<String, Object>, Void, ResultUser> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO处理底层获取后的数据
		 * @param result
		 * @return void
		 * @author zhuw
		 * @date 2013-4-24 上午10:19:30
		 */
		public abstract void procEditPrivacyData(User data);

	}

	CallBack callback;

	public UserPrivacyEditTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultUser doInBackground(HashMap<String, Object>... params) {
		ResultUser data = null;
		try {
			HashMap<String, Object> map = params[0];
			String userid = (String) map.get("id");
			ArrayList<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
			if (map != null && !map.isEmpty()) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (!entry.getKey().equals("id")) {
						
						if(entry.getValue().toString().equals("true")){
							
							paramPairs.add(new BasicNameValuePair(entry.getKey(),
									"1"));
						}else{
							paramPairs.add(new BasicNameValuePair(entry.getKey(),
									"0"));
						}
					
					}
				}
			}
			ResEntity re = NetConUtil.postByForm(ServerPath.PRICAY_EDIT_PATH
					+ userid, paramPairs);
			if (re.code == ResEntity.S_OK) {
				User user = JsonUtil.parseUser(new JSONArray(re.jsonStr).getJSONObject(0));
				data = new ResultUser(CommonData.H_FLAG_SUCCESS, null, user);
			} else if (re.code == ResEntity.S_FAILED) {

				data = new ResultUser(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + "    " + re.jsonStr, null);

			} else {
				data = new ResultUser(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + ErrorCode.ERROR_MSG, null);
			}

		} catch (Exception e) {
			data = new ResultUser(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG,
					null);
			e.printStackTrace();

		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultUser result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procEditPrivacyData(result.data);

		} else {
			callback.procError(result.msg);
		}

	}
}
